home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
mail.altrad.com
/
2015.02.mail.altrad.com.tar
/
mail.altrad.com
/
TEST
/
office deutch
/
INFOPATH.NL-NL
/
INFLR.CAB
/
SVCREQ.XSN_1043
/
script.js
< prev
next >
Wrap
Text File
|
2006-11-12
|
22KB
|
701 lines
/*
* This file contains functions for data validation and form-level events.
* Because the functions are referenced in the form definition (.xsf) file,
* it is recommended that you do not modify the name of the function,
* or the name and number of arguments.
*
*/
// The following line is created by Microsoft Office InfoPath to define the
// prefixes for all the known namespaces in the main XML data file.
// Any modification to the form files made outside of InfoPath
// will not be autmatically updated.
//<namespacesDefinition>
XDocument.DOM.setProperty("SelectionNamespaces", 'xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD" xmlns:svc="http://schemas.microsoft.com/office/infopath/2003/sample/ServiceRequest"');
//</namespacesDefinition>
/*------------------------------------------------------------------------------
This function handler is generated and managed automatically.
Do not rename the function or alter its parameter list.
------------------------------------------------------------------------------*/
function XDocument::OnLoad(oEvent)
{
updateDropDown(true /*isOnLoad*/, false /*isUndoRedo*/, XDocument.IsSigned /*isSigned*/);
// Avoid DOM updates when the document has been digitally signed.
if (XDocument.IsSigned)
return;
var today = new Date();
initializeNodeValue("/svc:serviceRequest/svc:dateOpened", getDateString(today));
return;
}
//=======
// The following function handler is created by Microsoft Office InfoPath.
// Do not modify the name of the function or the name and number of arguments.
//=======
function msoxd__service::OnAfterChange(eventObj)
{
updateDropDown(false /*isOnLoad*/, eventObj.IsUndoRedo, XDocument.IsSigned);
}
function updateDropDown(isOnLoad, isUndoRedo, isSigned)
{
//get the auxDOM
var oScratchDOM = XDocument.GetDOM('map');
oScratchDOM.setProperty("SelectionNamespaces", 'xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD" xmlns:svc="http://schemas.microsoft.com/office/infopath/2003/sample/ServiceRequest"');
//get the current service selection
var xmlService = XDocument.DOM.selectSingleNode("/svc:serviceRequest/svc:issue/svc:service");
var strService = xmlService.nodeTypedValue;
var xmlProblem = XDocument.DOM.selectSingleNode("/svc:serviceRequest/svc:issue/svc:problem");
var strProblem = xmlService.nodeTypedValue;
if (!isOnLoad && !isUndoRedo && !isSigned)
{
//reset the problem selection
setNodeValue(XDocument.DOM.selectSingleNode("/svc:serviceRequest/svc:issue/svc:problem"),'');
}
//the rest of the changes only apply to visuals, not the document, so we can make them
//in order to correctly display the current data
//clean up the scratchDOM
var oSelection = oScratchDOM.selectNodes("/svc:serviceMap/svc:selectedService/*");
oSelection.removeAll();
var xmlTopElement = oScratchDOM.selectSingleNode("/svc:serviceMap/svc:selectedService");
var xmlSelectedServiceNode = oScratchDOM.selectSingleNode("/svc:serviceMap/svc:service[@svc:value=" + createStringLiteral(strService) + "]");
if (xmlSelectedServiceNode)
{
xmlTopElement.appendChild(xmlSelectedServiceNode.cloneNode(true));
}
}
function createStringLiteral(strValue)
{
if (-1 == strValue.indexOf("'"))
return "'" + strValue + "'";
var rgPieces = strValue.split("'");
return "concat('" + rgPieces.join("', \"'\", '") + "')";
}
/*------------------------------------------------------------------------------
Common form template functions section - the following code is common to all out-of-box form templates.
------------------------------------------------------------------------------*/
/* =============================================================================
Currency handling
============================================================================= */
/*------------------------------------------------------------------------------
updateCurrency()
------------------------------------------------------------------------------*/
function updateCurrency(xmlNode, strPrefix)
{
var xmlName = xmlNode.selectSingleNode(strPrefix + 'name');
var xmlSymbol = xmlNode.selectSingleNode(strPrefix + 'symbol');
var strName = xmlName.text;
var strSymbol = lookupCurrencySymbol(strName);
if (strSymbol)
{
setNodeValue(xmlSymbol, '(' + strSymbol + ') ');
}
else
{
setNodeValue(xmlName, '');
setNodeValue(xmlSymbol, '');
}
}
/*------------------------------------------------------------------------------
lookupCurrencySymbol()
------------------------------------------------------------------------------*/
function lookupCurrencySymbol(strName)
{
var xmlCurDom = XDocument.GetDOM("currencies");
var xmlCurrency = xmlCurDom.selectSingleNode("/currencies/currency[@name = '" + strName + "']");
if (xmlCurrency)
return xmlCurrency.getAttribute("symbol");
}
/* =============================================================================
Date/Time handling
============================================================================= */
/*------------------------------------------------------------------------------
getDateString()
------------------------------------------------------------------------------*/
function getDateString(oDate)
{
// Use today as default value.
if (oDate == null)
oDate = new Date();
var m = oDate.getMonth() + 1;
var d = oDate.getDate();
if (m < 10)
m = "0" + m;
if (d < 10)
d = "0" + d;
// ISO 8601 date (YYYY-MM-DD).
return oDate.getFullYear() + "-" + m + "-" + d;
}
/*------------------------------------------------------------------------------
getTimeString()
------------------------------------------------------------------------------*/
function getTimeString(oTime)
{
// Use now as default value.
if (oTime == null)
oTime = new Date();
var h = oTime.getHours();
var m = oTime.getMinutes();
var s = oTime.getSeconds();
var ms = oTime.getMilliseconds();
if (h < 10)
h = "0" + h;
if (m < 10)
m = "0" + m;
if (s < 10)
s = "0" + s;
if (ms < 100)
ms = "0" + (ms < 10 ? "0" : "") + ms;
// ISO 8601 time (HH:MM:SS.SSS).
return h + ":" + m + ":" + s + "." + ms;
}
/* =============================================================================
Language conversion operations
============================================================================= */
/*------------------------------------------------------------------------------
convertXMLNumberToJScript()
------------------------------------------------------------------------------*/
function convertXMLNumberToJScript(value)
{
var retVal;
switch (value)
{
case "-INF":
retVal = Number.NEGATIVE_INFINITY;
break;
case "INF":
retVal = Number.POSITIVE_INFINITY;
break;
case "NaN":
retVal = NaN;
break;
default:
//If no prefix of its argument can be parsed as a
//float, parseFloat will return NaN. Otherwise, it
//will return the value of the longest float prefix
//it can find in its argument.
retVal = parseFloat(value);
}
return retVal;
}
/*------------------------------------------------------------------------------
convertJScriptNumberToXML()
------------------------------------------------------------------------------*/
function convertJScriptNumberToXML(value)
{
var retVal;
switch (value)
{
case Number.NEGATIVE_INFINITY:
retVal = "-INF";
break;
case Number.POSITIVE_INFINITY:
retVal = "INF";
break;
//NaN == NaN returns false. Any other value is equal to itself,
//which is why the case below differentiates between NaN and
//any other valid value
case value:
retVal = value;
break;
default:
retVal = "NaN";
}
return retVal;
}
/* =============================================================================
Node value operations
============================================================================= */
/*------------------------------------------------------------------------------
isInvalidOrEmpty()
------------------------------------------------------------------------------*/
function isInvalidOrEmpty(xmlNode)
{
// If there is no value, ignore it.
if (!xmlNode || !xmlNode.text)
return true;
// The caller can pass additional error types as optional arguments.
var aErrorTypes = new Array;
if (arguments.length > 1)
{
for (var i=1; i<arguments.length; i++)
aErrorTypes.push(arguments[i]);
}
else
{
aErrorTypes.push("SCHEMA_VALIDATION");
}
// If there is a validation error related to this node,
// then the node is invalid.
for (var i=0; i<XDocument.Errors.Count; i++)
{
var oError = XDocument.Errors(i);
if (xmlNode == oError.Node)
{
for (var j in aErrorTypes)
{
if (oError.Type == aErrorTypes[j])
return true;
}
}
}
// Is valid (no error was found).
return false;
}
/*------------------------------------------------------------------------------
getNodeValue()
------------------------------------------------------------------------------*/
function getNodeValue(xpath, defaultValue)
{
var xmlNode = getNode(xpath);
if (isInvalidOrEmpty(xmlNode))
return (arguments.length > 1) ? defaultValue : "";
else
return xmlNode.text;
}
/*------------------------------------------------------------------------------
getNodeTypedValue() - Use instead of getNodeValue() if you want
to parse the value by MSXML according to data type (from schema).
NOTE: this is not really useful for dates in Jscript.
------------------------------------------------------------------------------*/
function getNodeTypedValue(xpath, defaultValue, fNumber)
{
var xmlNode = getNode(xpath);
if (isInvalidOrEmpty(xmlNode))
return (arguments.length > 1) ? defaultValue : "";
if (fNumber)
return convertXMLNumberToJScript(xmlNode.nodeTypedValue);
else
return xmlNode.nodeTypedValue;
}
/*------------------------------------------------------------------------------
setNodeValue()
------------------------------------------------------------------------------*/
function setNodeValue(xpath, value)
{
var xmlNode = getNode(xpath);
if (!xmlNode)
return;
// The xsi:nil needs to be removed before we set the value.
if (value != "" && xmlNode.getAttribute("xsi:nil"))
xmlNode.removeAttribute("xsi:nil");
// Setting the value would mark the document as dirty.
// Let's do that if the value has really changed.
if (xmlNode.text != value)
xmlNode.text = value;
}
/*------------------------------------------------------------------------------
setNodeTypedValue() - Use instead of setNodeValue() if you want
to format the value by MSXML according to data type (from schema).
Use this when setting floating point and decimal values. These
values are formatted differently in some locales and will cause schema
validation errors when set using setNodeValue or a node's text property.
NOTE: this is not really useful for dates in Jscript.
------------------------------------------------------------------------------*/
function setNodeTypedValue(xpath, value)
{
var xmlNode = getNode(xpath);
if (!xmlNode)
return;
// The xsi:nil needs to be removed before we set the value.
if (value != "" && xmlNode.getAttribute("xsi:nil"))
xmlNode.removeAttribute("xsi:nil");
var convertedValue = convertJScriptNumberToXML(value);
// Setting the value would mark the document as dirty.
// Let's do that if the value has really changed.
if (xmlNode.nodeTypedValue != convertedValue)
xmlNode.nodeTypedValue = convertedValue;
}
/*------------------------------------------------------------------------------
setRoundedValue() - Rounds the value to the default number of decimal
places and calls setNodeTypedValue() with the result.
------------------------------------------------------------------------------*/
function setRoundedValue(xpath, value)
{
var xmlNode = getNode(xpath);
if (!xmlNode)
return;
var roundedValue = roundFloat(value, 3);
setNodeTypedValue(xpath, roundedValue);
}
/*------------------------------------------------------------------------------
setNil() - Empty a nillable element.
------------------------------------------------------------------------------*/
function setNil(xpath)
{
var xmlNode = getNode(xpath);
if (!xmlNode || xmlNode.text === "")
return;
// Create xsi:nil attribute with the proper namespace.
var xmlNil = xmlNode.ownerDocument.createNode(2, "xsi:nil", "http://www.w3.org/2001/XMLSchema-instance");
xmlNil.text = "true";
// The order is important.
xmlNode.text = "";
xmlNode.setAttributeNode(xmlNil);
}
/*------------------------------------------------------------------------------
roundFloat() - Rounds the value to the specified number of decimal places.
------------------------------------------------------------------------------*/
function roundFloat(value, decimalPlaces)
{
if (value < -1E15 || 1E15 < value)
{
return value;
}
else
{
var nPowerToRound = Math.pow(10, decimalPlaces);
return Math.round(value*nPowerToRound)/nPowerToRound;
}
}
/*------------------------------------------------------------------------------
normalizeSource() - Ensures that we always get a node of type NODE_ELEMENT
or NODE_ATTRIBUTE.
------------------------------------------------------------------------------*/
function normalizeSource(oEvent)
{
// Return the node's parent if the node is of type NODE_TEXT and its parent
// is of type NODE_ELEMENT
if (oEvent.Parent.nodeType == 1 && oEvent.Source.nodeType == 3)
return oEvent.Parent;
// Return the node if it is of type NODE_ELEMENT or NODE_ATTRIBUTE;
if (oEvent.Source.nodeType == 1 || oEvent.Source.nodeType == 2)
return oEvent.Source;
}
/*------------------------------------------------------------------------------
normalizeParent() - Ensures that we always get the parent of a node of
type NODE_ELEMENT or NODE_ATTRIBUTE.
------------------------------------------------------------------------------*/
function normalizeParent(oEvent)
{
// Return the node if it is of type NODE_ELEMENT or NODE_ATTRIBUTE;
if (oEvent.Source.nodeType == 1 || oEvent.Source.nodeType == 2)
return oEvent.Parent;
// Return the node 's parent if the node is of type NODE_TEXT and its parent
// is of type NODE_ELEMENT
if (oEvent.Parent.nodeType == 1 && oEvent.Source.nodeType == 3)
return oEvent.Parent.parentNode;
}
/*------------------------------------------------------------------------------
initializeNodeValue()
------------------------------------------------------------------------------*/
function initializeNodeValue(xpath, strValue)
{
var xmlNode = getNode(xpath);
// Set the node value *ONLY* if the node is empty.
if (xmlNode.text == "")
setNodeValue(xmlNode, strValue);
}
/* =============================================================================
Simple nodelist operations
============================================================================= */
/*------------------------------------------------------------------------------
getNode()
------------------------------------------------------------------------------*/
function getNode(xpath)
{
// Both XML node and absolute XPath are allowed.
if (typeof(xpath) == "string")
return XDocument.DOM.selectSingleNode(xpath);
else
return xpath;
}
/*------------------------------------------------------------------------------
getNodeList()
------------------------------------------------------------------------------*/
function getNodeList(xpath)
{
// Both XML node and absolute XPath are allowed.
if (typeof(xpath) == "string")
return XDocument.DOM.selectNodes(xpath);
else
return xpath;
}
/*------------------------------------------------------------------------------
count()
------------------------------------------------------------------------------*/
function count(xpath)
{
var xmlNodeList = getNodeList(xpath);
if (xmlNodeList)
return xmlNodeList.length;
else
return -1;
}
/* =============================================================================
Complex nodelist operations
============================================================================= */
/*------------------------------------------------------------------------------
applyAction() - Iterate the whole nodelist and apply a given action
to each node in the nodelist. You can implement your own actions.
Example:
var oAction = new Object();
oAction.sum = 0;
oAction.apply = new Function("xmlNode", "this.sum += xmlNode.nodeTypedValue");
------------------------------------------------------------------------------*/
function applyAction(xpath, oAction)
{
var xmlNodeList = getNodeList(xpath);
var xmlNode;
while (xmlNode = xmlNodeList.nextNode())
oAction.apply(xmlNode);
xmlNodeList.reset();
}
/*------------------------------------------------------------------------------
sum()
------------------------------------------------------------------------------*/
function sum(xpath)
{
var oAction = new Object;
oAction.nResult = 0;
oAction.apply = new Function("xmlNode", "this.nResult += getNodeTypedValue(xmlNode, 0, true)");
applyAction(xpath, oAction);
return oAction.nResult;
}
/*------------------------------------------------------------------------------
anyInvalidOrEmpty() - Searches a nodelist to see if there are any invalid
or empty nodes.
------------------------------------------------------------------------------*/
function anyInvalidOrEmpty(xpath)
{
var oAction = new Object;
oAction.fInvalid = false;
oAction.apply = new Function("xmlNode", "this.fInvalid |= isInvalidOrEmpty(xmlNode)");
applyAction(xpath, oAction);
return oAction.fInvalid;
}
/*------------------------------------------------------------------------------
reindex()
------------------------------------------------------------------------------*/
function reindex(xpath, iStart)
{
if (iStart == null)
iStart = 1;
var oAction = new Object;
oAction.i = iStart;
oAction.apply = new Function("xmlNode", "setNodeTypedValue(xmlNode, this.i++)");
return applyAction(xpath, oAction);
}
/*------------------------------------------------------------------------------
average()
------------------------------------------------------------------------------*/
function average(xpath, xpathWeights)
{
var oAction = new Object;
oAction.nSum = 0;
oAction.cNodes = 0;
if (!xpathWeights)
{
oAction.apply = actionSimpleAverage;
oAction.result = resultSimpleAverage;
}
else
{
oAction.nWeights = 0;
oAction.xmlWeights = getNodeList(xpathWeights);
oAction.apply = actionWeightedAverage;
oAction.result = resultWeightedAverage;
}
applyAction(xpath, oAction);
return oAction.result();
}
/*------------------------------------------------------------------------------
actionSimpleAverage()
------------------------------------------------------------------------------*/
function actionSimpleAverage(xmlNode)
{
if (!isInvalidOrEmpty(xmlNode))
{
this.nSum += getNodeTypedValue(xmlNode, 0, true);
this.cNodes++;
}
}
/*------------------------------------------------------------------------------
resultSimpleAverage()
------------------------------------------------------------------------------*/
function resultSimpleAverage()
{
if (this.cNodes)
return this.nSum / this.cNodes;
else
return 0;
}
/*------------------------------------------------------------------------------
actionWeightedAverage()
------------------------------------------------------------------------------*/
function actionWeightedAverage(xmlNode)
{
// There should be one weight for each node. Even if the node is invalid,
// we have to advance the iterator of weights to keep them in sync.
var xmlWeight = this.xmlWeights.nextNode();
if (!isInvalidOrEmpty(xmlNode) && !isInvalidOrEmpty(xmlWeight))
{
var nValue = getNodeTypedValue(xmlNode, 0, true);
var nWeight = getNodeTypedValue(xmlWeight, 1, true);
this.nSum += nValue * nWeight;
this.nWeights += nWeight;
this.cNodes++;
}
}
/*------------------------------------------------------------------------------
resultWeightedAverage()
------------------------------------------------------------------------------*/
function resultWeightedAverage()
{
this.xmlWeights.reset();
if (this.cNodes)
{
if (this.nWeights > 0)
return this.nSum / this.nWeights;
else
return this.nSum
}
else
{
return 0;
}
}
/*------------------------------------------------------------------------------
sort()
------------------------------------------------------------------------------*/
function sort(xpathParent, xpathChildren, fnCompare)
{
// Retrieve the parent node; all the children will be sorted.
var xmlOrigParent = getNode(xpathParent)
var xmlItems = xmlOrigParent.selectNodes(xpathChildren);
if (1 < count(xmlItems))
{
// Store the (pointers to) items in an array for faster access.
rgItems = new Array();
while (xmlItem = xmlItems.nextNode())
rgItems.push(xmlItem);
// Sort the array (the XML does not change).
rgItems.sort(fnCompare);
// Now that the array is sorted the DOM should be updated.
var xmlSortParent = xmlOrigParent.cloneNode(true);
var xmlClones = xmlSortParent.selectNodes(xpathChildren);
xmlClones.removeAll();
// Update the nodelist, each item is moved only once.
// (each change is causing several notifications to be fired.)
for (var i=0; i<rgItems.length; i++)
xmlSortParent.appendChild(rgItems[i].cloneNode(true));
xmlOrigParent.parentNode.replaceChild(xmlSortParent, xmlOrigParent);
}
}